define(function (require, exports, module) {
//    require("echart/echart");

    var ec = require("echarts/echarts");
//    var themes = require("echarts/theme/gray");
    require("zrender/zrender");
    require("apps/easyui");
    function renderPie(dom, data){
        var option = {
            title: {
                text: '工艺参数记录统计',
                x: 'right'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
//            legend: {
//                orient: 'vertical',
//                x: 'left',
//                data: []
//            },
            calculable: false,
            series: [
                {
                    name: '工艺名称',
                    type: 'pie',
                    radius: '45%',
                    center: ['50%', '50%'],
                    data: []
                }
            ]
        };
        var _data = data.processCounter;
        for(var i in _data){
//            option.legend.data.push(i);
            option.series[0].data.push({
                name:i,
                value:_data[i]
            })
        }
        var myChart = ec.init(dom);
        myChart.setOption(option);
        $("#globContent").on("resize", myChart.resize);
		$(window).on("resize", myChart.resize);
        return myChart;
    }

    function renderLine(dom, data){
        var option = {
            tooltip : {
                trigger: 'axis'
            },
//            legend: {
//                data:[]
//            },
            calculable : false,
            xAxis : [
                {
                    type : 'category',
                    boundaryGap : false,
                    data : []
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : [

            ]
        };
        var _data = data.timeDataCounter;
        var renderTime = false;
        for(var i in _data){
//            option.legend.data.push(i);
            var ser = {
                name:i,
                type:'line',
                data:[]
            };
            for(var j in _data[i]){
                if(!renderTime){
                    option.xAxis[0].data.push(j);
                }
                ser.data.push(_data[i][j]);
            }
            option.series.push(ser);
            renderTime = true;
        }
        var myChart = ec.init(dom);
        myChart.setOption(option);
        $("#globContent").on("resize", myChart.resize);
		$(window).on("resize", myChart.resize);
        return myChart;
    }

    var chartsMethods = [
        renderPie,
        renderLine
    ];

    exports.init = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);

        var current = new Date(config.currentTime);
        page.getJqDom("calendar").html(current.format("yyyy年MM月dd日"));
        timeRender(page.getJqDom("time"), config.currentTime);
        function timeRender(dom, now){
            now += 1000;
            dom.html('<i class="fa fa-clock-o"></i>' + new Date(now).format("hh:mm:ss"));
            setTimeout(function(){
                timeRender(dom, now);
            }, 1000);
        }
        $.getJSON(ctx + "/app/api/lab/recordCount", function (data) {
            var echarts = [];
            page.getJqDom("chart").each(function(i, item){
                echarts.push(chartsMethods[i](item, data));
            });
            echarts[0].connect(echarts[1]);
            echarts[1].connect(echarts[0]);
        })
    }
});